<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="优惠券ID" prop="couponId">
      <el-input v-model="dataForm.couponId" placeholder="优惠券ID"></el-input>
    </el-form-item>
    <el-form-item label="乘客ID" prop="customerId">
      <el-input v-model="dataForm.customerId" placeholder="乘客ID"></el-input>
    </el-form-item>
    <el-form-item label="优化券状态（1：未使用 2：已使用）" prop="status">
      <el-input v-model="dataForm.status" placeholder="优化券状态（1：未使用 2：已使用）"></el-input>
    </el-form-item>
    <el-form-item label="领取时间" prop="receiveTime">
      <el-input v-model="dataForm.receiveTime" placeholder="领取时间"></el-input>
    </el-form-item>
    <el-form-item label="使用时间" prop="usedTime">
      <el-input v-model="dataForm.usedTime" placeholder="使用时间"></el-input>
    </el-form-item>
    <el-form-item label="订单id" prop="orderId">
      <el-input v-model="dataForm.orderId" placeholder="订单id"></el-input>
    </el-form-item>
    <el-form-item label="过期时间" prop="expireTime">
      <el-input v-model="dataForm.expireTime" placeholder="过期时间"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="删除标记（0:不可用 1:可用）" prop="isDeleted">
      <el-input v-model="dataForm.isDeleted" placeholder="删除标记（0:不可用 1:可用）"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          couponId: '',
          customerId: '',
          status: '',
          receiveTime: '',
          usedTime: '',
          orderId: '',
          expireTime: '',
          createTime: '',
          updateTime: '',
          isDeleted: ''
        },
        dataRule: {
          couponId: [
            { required: true, message: '优惠券ID不能为空', trigger: 'blur' }
          ],
          customerId: [
            { required: true, message: '乘客ID不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '优化券状态（1：未使用 2：已使用）不能为空', trigger: 'blur' }
          ],
          receiveTime: [
            { required: true, message: '领取时间不能为空', trigger: 'blur' }
          ],
          usedTime: [
            { required: true, message: '使用时间不能为空', trigger: 'blur' }
          ],
          orderId: [
            { required: true, message: '订单id不能为空', trigger: 'blur' }
          ],
          expireTime: [
            { required: true, message: '过期时间不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '删除标记（0:不可用 1:可用）不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/customercoupon/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 200) {
                this.dataForm.couponId = data.customerCoupon.couponId
                this.dataForm.customerId = data.customerCoupon.customerId
                this.dataForm.status = data.customerCoupon.status
                this.dataForm.receiveTime = data.customerCoupon.receiveTime
                this.dataForm.usedTime = data.customerCoupon.usedTime
                this.dataForm.orderId = data.customerCoupon.orderId
                this.dataForm.expireTime = data.customerCoupon.expireTime
                this.dataForm.createTime = data.customerCoupon.createTime
                this.dataForm.updateTime = data.customerCoupon.updateTime
                this.dataForm.isDeleted = data.customerCoupon.isDeleted
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/customercoupon/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'couponId': this.dataForm.couponId,
                'customerId': this.dataForm.customerId,
                'status': this.dataForm.status,
                'receiveTime': this.dataForm.receiveTime,
                'usedTime': this.dataForm.usedTime,
                'orderId': this.dataForm.orderId,
                'expireTime': this.dataForm.expireTime,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'isDeleted': this.dataForm.isDeleted
              })
            }).then(({data}) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
